from datetime import datetime, timedelta, timezone
import uuid  # Import the uuid library
from utils import get_lunar_and_solar_terms  # 导入utils中的方法
import time  # 导入time库

# 创建ICS文件内容
ics_content = """BEGIN:VCALENDAR
VERSION:2.0
PRODID:-//Your Company//Your Product//EN
CALSCALE:GREGORIAN
METHOD:PUBLISH
X-WR-CALNAME:2025年春运火车票购票日历
X-WR-TIMEZONE:Asia/Shanghai
BEGIN:VTIMEZONE
TZID:Asia/Shanghai
BEGIN:STANDARD
DTSTART:19700101T000000
TZOFFSETFROM:+0800
TZOFFSETTO:+0800
END:STANDARD
END:VTIMEZONE
"""

# 开始日期和结束日期
start_date = datetime.now()
end_date = start_date + timedelta(days=30)

# 生成日历事件
current_date = start_date
while current_date <= end_date:
    sale_date = current_date + timedelta(days=14)
    
    # 获取农历和节气信息
    date_str = sale_date.strftime('%Y%m%d')
    lunar_calendar, solar_terms = get_lunar_and_solar_terms(date_str)
    lunar_info = lunar_calendar if lunar_calendar else "未知农历日期"
    if solar_terms and "后" not in solar_terms:
        lunar_info += f" ({solar_terms})"
    
    event_uid = str(uuid.uuid4())
    dtstamp = datetime.now(timezone.utc).strftime('%Y%m%dT%H%M%SZ')
    ics_content += f"""
BEGIN:VEVENT
UID:{event_uid}
DTSTAMP:{dtstamp}
DTSTART;TZID=Asia/Shanghai:{current_date.strftime('%Y%m%dT080000')}
DTEND;TZID=Asia/Shanghai:{current_date.strftime('%Y%m%dT083000')}
SUMMARY:开售{sale_date.strftime('%Y年%m月%d日')}车票
DESCRIPTION:{lunar_info}
END:VEVENT
"""
    current_date += timedelta(days=1)
    
    # 控制请求间隔
    time.sleep(2)  # 每次请求后等待2秒

ics_content += "\nEND:VCALENDAR"

# 将ICS内容写入文件
with open("2025_spring_festival_ticket_sales.ics", "w", encoding="utf-8") as f:
    f.write(ics_content)

print("ICS文件已生成：spring_festival_ticket_sales.ics")